home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / standards / sgml / nist / parse1 / dtdumisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-13  |  12.7 KB  |  475 lines

  1. /* National Institute of Standards and Technology (NIST)
  2. /* National Computer System Laboratory (NCSL)
  3. /* Office Systems Engineering (OSE) Group
  4. /* ********************************************************************
  5. /*                            D I S C L A I M E R
  6. /*                              (March 8, 1989)
  7. /*  
  8. /* There is no warranty for the NIST NCSL OSE SGML parser and/or the NIST
  9. /* NCSL OSE SGML parser validation suite.  If the SGML parser and/or
  10. /* validation suite is modified by someone else and passed on, NIST wants
  11. /* the parser's recipients to know that what they have is not what NIST
  12. /* distributed, so that any problems introduced by others will not
  13. /* reflect on our reputation.
  14. /* 
  15. /* Policies
  16. /* 
  17. /* 1. Anyone may copy and distribute verbatim copies of the SGML source
  18. /* code as received in any medium.
  19. /* 
  20. /* 2. Anyone may modify your copy or copies of SGML parser source code or
  21. /* any portion of it, and copy and distribute such modifications provided
  22. /* that all modifications are clearly associated with the entity that
  23. /* performs the modifications.
  24. /* 
  25. /* NO WARRANTY
  26. /* ===========
  27. /* 
  28. /* NIST PROVIDES ABSOLUTELY NO WARRANTY.  THE SGML PARSER AND VALIDATION
  29. /* SUITE ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  30. /* EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  31. /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  32. /* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS
  33. /* WITH YOU.  SHOULD THE SGML PARSER OR VALIDATION SUITE PROVE DEFECTIVE,
  34. /* YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  35. /* 
  36. /* IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL NIST BE LIABLE FOR
  37. /* DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR OTHER SPECIAL,
  38. /* INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR
  39. /* INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA
  40. /* BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR A
  41. /* FAILURE OF THE PROGRAM TO OPERATE WITH PROGRAMS NOT DISTRIBUTED BY
  42. /* NIST) THE PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF
  43. /* SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  44. */
  45.  
  46. /************************************************************************/
  47. /*   TITLE:          SGML PARSER                                        */
  48. /*   SYSTEM:         DTD PROCESSOR                                      */
  49. /*   SUBSYSTEM:                                                         */
  50. /*   SOURCE FILE:    DTUMISC.C                                          */
  51. /*   AUTHOR:         Jim Heath                                          */
  52. /*                                                                      */
  53. /*   DATE CREATED:                                                      */
  54. /*   LAST MODIFIED:                                                     */
  55. /*                                                                      */
  56. /*                  REVISIONS                                           */
  57. /*   WHEN      WHO            WHY                                       */
  58. /************************************************************************/
  59. #include <stdio.h>
  60. #include <setjmp.h>
  61. #include <fcntl.h>
  62. #include <sys/types.h>
  63. #include <sys/stat.h>
  64. #include <ctype.h>
  65. #include "qntyset.h"
  66. #include "dtd.h"
  67. #include "dtdfncs.h"
  68. #include "dtdglbl.h"
  69.  
  70. /* ============================================================ */
  71. void nullfnc()
  72. {
  73. }
  74. /* ============================================================ */
  75. int noxlat(x)
  76. int x;
  77. {
  78.    return(x);
  79. }
  80.  
  81. /* ============================================================ */
  82. void FUNCTRACE(x)
  83. char *x;
  84. {
  85.    extern int debug;
  86.  
  87.    if (debug & FUNCTRC)
  88.       printf("%s\n", x);
  89. }
  90. /* ============================================================ */
  91. void terminate(code, msg)
  92. int code;
  93. char *msg;
  94. {
  95.    char *decl;
  96.    extern  int errflag;
  97.  
  98.    GETDECLADDR(&decl);
  99.    printf("\n\n-------------- F A T A L    E R R O R --------------\n");
  100.    printf("in document:  %s\n", docfname);
  101.    printf("code = %d, msg = %s\n", code, msg);
  102.    printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
  103.    closeall();
  104.    unlinkall(TRUE);
  105.    if ((debug & WAITFORCR) != FALSE){
  106.       printf("\n Press enter to continue -\n");
  107.       (void) getchar();
  108.    }
  109.    exit(code);
  110. }
  111. /* ============================================================ */
  112. int TOUPPER(x)
  113. REGISTER int x;
  114. {
  115.    int j;
  116.    if (isascii(x) && isalpha(x) && islower(x)) {
  117.       j = toupper(x);
  118.       return(j);
  119.    }
  120.    return(x & 0xFF);
  121. }
  122. /* ============================================================ */
  123. /* ============================================================ */
  124. void syntxerr(msg)
  125. char *msg;
  126. {
  127.    char *decl;
  128.    extern  int errflag;
  129.    extern jmp_buf       Xenv;
  130.    static int errcount = 0;
  131.  
  132.    GETDECLADDR(&decl);
  133.    specflag = 0;
  134.    errflag = 1;
  135.    printf("\n\n-------------- E R R O R --------------\n");
  136.    printf("in document:  %s\n", docfname);
  137.    printf("%s\n", msg);
  138.    printf("Current Declaration = \n%s\n", decl);
  139.    printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
  140.  
  141.    if ((debug & WAITFORCR) != FALSE){
  142.       printf("\n Press enter to continue -\n");
  143.       (void) getchar();
  144.    }
  145.    if ((debug & ALLOWSYNTXERRS) == ALLOWSYNTXERRS)
  146.       longjmp(Xenv, 1);
  147.    else
  148.       exit(1);
  149. }
  150. /* ============================================================ */
  151. int ISALPHA(c)
  152. REGISTER int c;
  153. {
  154.    if (isascii(c) && isalpha(c))
  155.       return(TRUE);
  156.    return(FALSE);
  157. }
  158. /* ============================================================ */
  159. int ISDIGIT(c)
  160. REGISTER int c;
  161. {
  162.    if (isascii(c) && isdigit(c))
  163.       return(TRUE);
  164.    return(FALSE);
  165. }
  166. /* ============================================================ */
  167. int ISALNUM(c)
  168. REGISTER int c;
  169. {
  170.    if (isascii(c) && (isalpha(c) || isdigit(c)))
  171.       return(TRUE);
  172.    return(FALSE);
  173. }
  174. /* ============================================================ */
  175. int isnmchar(c)
  176. REGISTER int c;
  177. {
  178.    if (ISALNUM(c) || (c == '.') || (c == '-'))
  179.       return(TRUE);
  180.    return(FALSE);
  181. }
  182. /* ============================================================ */
  183. int isnmstrt(c)
  184. REGISTER int c;
  185. {
  186.    if (isascii(c) && isalpha(c))
  187.       return(TRUE);
  188.    return(FALSE);
  189. }
  190. /* ============================================================ */
  191. int isspcl(j)
  192. REGISTER int j;
  193. {
  194.    switch(j) {
  195.    case GRPO:
  196.    case GRPC:
  197.    case '+':
  198.    case ',':
  199.    case '-':
  200.    case '.':
  201.    case '/':
  202.    case ':':
  203.    case '=':
  204.    case '?':
  205.       return(TRUE);
  206.    default:
  207.       return(FALSE);
  208.    }
  209. }
  210. /* ============================================================ */
  211. void safewrite(file, buff, len)
  212. int file;
  213. char *buff;
  214. unsigned len;
  215. {
  216.     int stat;
  217.    if ((stat = write(file, buff, len)) != len){
  218.         perror("error in writing to file\n");
  219.       terminate(1, "Error in writing to file");
  220.     }
  221. }
  222. /* ============================================================ */
  223. int safeopen(fname, mode, maskbit)
  224. char *fname;
  225. int mode, maskbit;
  226. {
  227.    extern int debug;
  228.    int temp;
  229.    if (filemask & maskbit)
  230.       goto ERROR1;
  231.    filemask |= maskbit;
  232.    if (debug & FILETRC)
  233.       FPRINTF((stderr,"opening %s\n", fname));
  234. #ifdef OLD
  235.    mode = O_RDWR;
  236. #endif
  237.    if ((temp = open(fname, mode)) !=  -1)
  238.       return(temp);
  239.     perror("open failed\n");
  240. ERROR1:
  241.    FPRINTF((stderr, "error in opening %s\n", fname));
  242.    closeall();
  243.    unlinkall(TRUE);
  244.    exit(1);
  245. }
  246. /* ============================================================ */
  247. int safecreat(fname, maskbit)
  248. char *fname;
  249. int maskbit;
  250. {
  251.    /*#define CREATOPTS (O_CREAT|O_TRUNC|O_BINARY|S_IWRITE|S_IREAD)*/
  252. #define CREATOPTS (O_CREAT|O_TRUNC|S_IWRITE|S_IREAD)
  253.    extern int debug;
  254.    int temp;
  255.    if (filemask & maskbit)
  256.       goto ERROR1;
  257.    filemask |= maskbit;
  258.    if (debug & FILETRC)
  259.       FPRINTF((stderr,"creating %s\n", fname));
  260.    if ((temp = creat(fname, CREATOPTS)) !=  -1)
  261.       return(temp);
  262. ERROR1:
  263.    printf("Filename = %s :Filemask = %d : Maskbit = %d\n",fname,filemask,maskbit);
  264.    FPRINTF((stderr, "error in creating %s\n", fname));
  265.    closeall();
  266.    unlinkall(TRUE);
  267.    exit(1);
  268. }
  269. /* ============================================================ */
  270. int safeunlink(fname)
  271. char *fname;
  272. {
  273.    extern int debug;
  274.    if (unlink(fname) !=  -1)
  275.       return(0);
  276.    return(-1);
  277. }
  278. /* ============================================================ */
  279. FILE *safefopen(fname, mode, maskbit)
  280. char *fname;
  281. char *mode;
  282. int maskbit;
  283. {
  284.    char mymode[32];
  285.    extern int debug;
  286.    FILE *temp, *fopen();
  287.    if (debug & FILETRC)
  288.       FPRINTF((stderr,"fopening %s\n", fname));
  289.    if (filemask & maskbit)
  290.       goto ERROR1;
  291.    filemask |= maskbit;
  292.    strcpy(mymode,mode);
  293.    strcat(mymode,"b");
  294.    if ((temp = fopen(fname, mymode)) !=  NULL)
  295.       return(temp);
  296. ERROR1:
  297.    FPRINTF((stderr, "error in fopening %s\n", fname));
  298.    closeall();
  299.    unlinkall(TRUE);
  300.    exit(1);
  301. }
  302. /* ============================================================ */
  303. void safeclose(fd, fname, maskbit)
  304. int fd;
  305. char *fname;
  306. int maskbit;
  307. {
  308.    extern int debug;
  309.    if (debug & FILETRC)
  310.       FPRINTF((stderr,"closing %s\n", fname));
  311.    if ((filemask & maskbit) == 0)
  312.       goto ERROR1;
  313.    filemask &= (~maskbit);
  314.    if (close(fd) != -1)
  315.       return;
  316. ERROR1:
  317.    printf("error in closing %s\n", fname);
  318.    exit(1);
  319. }
  320. /* ============================================================ */
  321. void safefclose(fp, fname, maskbit)
  322. FILE *fp;
  323. char *fname;
  324. int maskbit;
  325. {
  326.    extern int debug;
  327.    if (debug & FILETRC)
  328.       FPRINTF((stderr,"closing %s\n", fname));
  329.    if ((filemask & maskbit) == 0)
  330.       goto ERROR1;
  331.    filemask &= (~maskbit);
  332.    if (fclose(fp) == 0)
  333.       return;
  334. ERROR1:
  335.    printf("error in closing %s\n", fname);
  336.    exit(1);
  337. }
  338. /* ============================================================ */
  339. char *synliteral(j)
  340. REGISTER int j;
  341. {
  342.    switch (j){
  343.    case KW_PUBLIC:
  344.       return("PUBLIC");
  345.    case KW_SYSTEM:
  346.       return("SYSTEM");
  347.    case KW_CDATA:
  348.       return("CDATA");
  349.    case KW_SDATA:
  350.       return("SDATA");
  351.    case KW_PI:
  352.       return("PI");
  353.    case KW_STARTTAG:
  354.       return("STARTTAG");
  355.    case KW_ENDTAG:
  356.       return("ENDTAG");
  357.    case KW_MS:
  358.       return("MS");
  359.    case KW_MD:
  360.       return("MD");
  361.    default:
  362.       syntxerr("Unknown syntactic literal");
  363.    }
  364.    /* should never be reached, for lint only */
  365.    return("");
  366. }
  367. /* ============================================================ */
  368. void mprintf(ptr)
  369. char *ptr;
  370. {
  371. }
  372. /* ============================================================ */
  373. int getnamegrp(temp, firstchar, otherchar)
  374. char *temp;
  375. int (*firstchar)(), (*otherchar)();
  376. {
  377.    REGISTER char *src, *dest;
  378.    char *grp, *dptr;
  379.    int nmcount = 0, namelen;
  380.    enum {
  381.       START, STARTNAME, INNAME      }
  382.    state = START;
  383.  
  384.    memset(temp, '\0', (GRPCNT * (NAMELEN + 1)));
  385.    grp = getagroup(0);
  386.    for(src = dest = grp; *src != 0; src++){
  387.       if (isspcl(*src) || isconnector(*src) || isnmchar(*src))
  388.          *dest++ = *src;
  389.    }
  390.    *dest = '\0';
  391.  
  392.    for(src = grp, dest = dptr = temp;;src++){
  393.       switch(state) {
  394.       case START:
  395.          if (*src != GRPO)
  396.             syntxerr("expected GRPO");
  397.          state = STARTNAME;
  398.          break;
  399.       case STARTNAME:
  400.          if (!(*firstchar)(*src))
  401.             syntxerr("expected name start character");
  402.          nmcount++;
  403.          *dest++ = *src;
  404.          namelen = 1;
  405.          state = INNAME;
  406.          break;
  407.       case INNAME:
  408.          if ((*otherchar)(*src)) {
  409.             if(namelen >= NAMELEN)
  410.                syntxerr("name too long in group");
  411.             *dest++ = *src;
  412.             namelen++;
  413.             break;
  414.          }
  415.          else if (isconnector(*src)) {
  416.             dptr += NAMELEN + 1;
  417.             dest = dptr;
  418.             state = STARTNAME;
  419.             break;
  420.          }
  421.          else if (*src == GRPC) {
  422.             return(nmcount);
  423.          }
  424.       }
  425.    }
  426. }
  427. /* =========================================================== */
  428. int isconnector(c)
  429. REGISTER char c;
  430. {
  431.    if ((c == '&') || (c == '|') || (c == ','))
  432.       return(TRUE);
  433.    return(FALSE);
  434. }
  435. /* =========================================================== */
  436. int isoi(c)
  437. REGISTER char c;
  438. {
  439.    if ((c == '?') || (c == '+') || (c == '*'))
  440.       return(TRUE);
  441.    return(FALSE);
  442. }
  443. /* ============================================================ */
  444. int isnamerni(buff, len)
  445. REGISTER char *buff;
  446. int len;
  447. {
  448.    REGISTER int j;
  449.    if (strncmp(buff, "#PCDATA", 7) == 0)
  450.       return(TRUE);
  451.    if (!isnmchar(*buff))
  452.       return(FALSE);
  453.    for (j = 0; j < (len + 1); j++, buff++)
  454.       if(!(isnmchar(*buff)))
  455.          break;
  456.    if ((j == len) && isnmchar(*buff))
  457.       return(FALSE);
  458.    return(TRUE);
  459. }
  460. /* ============================================================ */
  461. char *getnamerni(src)
  462. REGISTER char *src;
  463. {
  464.    static char namearray[NAMELEN + 1];
  465.    REGISTER char *dest = namearray;
  466.    memset(namearray, '\0', sizeof(namearray));
  467.    *dest++ = *src++;
  468.    while (isnmchar(*src))
  469.       *dest++ = *src++;
  470.    return(namearray);
  471. }
  472. /* ============================================================ */
  473. /* ============================================================ */
  474.  
  475.